<template>
  <div>
    <el-dialog
      :title="info.isadd ? '活动添加' : '活动编辑'"
      :visible.sync="info.isshow"
      @closed="closed"
      @opened="opened"
    >
      <!-- opend弹框打开事件 -->
      <el-form :model="user"
        >{{ user }}
        <el-form-item label="活动名称" label-width="120px">
          <el-input  v-model="user.title"></el-input>
        </el-form-item>
        <el-form-item label="活动期限" label-width="120px">
          <template>
            <div class="block">
             <!-- 时间 -->
              <el-date-picker
                v-model="value1"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                 @change="selectTime"
              >
              </el-date-picker>
            </div>
          </template>
        </el-form-item>
        <el-form-item label="一级分类" label-width="120px">
          <el-select v-model="user.first_cateid" @change="changeFirst">
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="二级级分类" label-width="120px">
          <el-select v-model="user.second_cateid" @change="getGoods">
            <el-option
              v-for="item in secondeCatelist"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
          
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品" label-width="120px">
          <el-select v-model="user.goodsid">
            <el-option
              v-for="item in goodslist"
              :key="item.id"
              :label="item.goodsname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add" v-if="info.isadd"
          >确 定添加</el-button
        >
        <el-button type="primary" @click="updata" v-else>确定修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import E from "wangeditor";
import { IndexRoutes } from "../../../router";
import {
  reqcatelist,
  reqgoodsadd,
  reqgoodslist,
  reqgoodsinfo,
  reqgoodsedit,
  reqsecksadd,
  reqseckinfo,
  reqseckedit,
} from "../../../http/api";
import { errorAlert, successAlert } from "../../../utils//alert";
import path from "path";
import specs from "../../../store/modules/specs";
export default {
  props: ["info"],
  data() {
    return {
      // 菜单列表出数值
      menuList: [],

      //   表单数据
      user: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 2,
      },
      value1: [],
      // 二级分类列表
      secondeCatelist: [],

      goodslist: "",

      //  规格属性列表
      // attrArr: [
      //   {
      //     val: "",
      //   },
      //   {
      //     val: "",
      //   },
      // ],
    };
  },
  computed: {
    ...mapGetters(
      // 商品列表数据
      {
        // 请求回来的一级分类列表
        cateList: "cate/list",
      }
    ),
  },
  methods: {
    // 分类下的数据请求
    ...mapActions({
      reqCateList: "cate/reqList",
      //商品列表
      reqList: "seckill/reqList",
    }),
    // 点击取消
    cancel() {
      this.info.isshow = false;
    
    },

    selectTime(e){
      // 处理时间
      this.user.begintime =
        e[0].getFullYear() +
        "/" +
        (e[0].getMonth() * 1 + 1) +
        "/" +
        e[0].getDate();
      this.user.endtime =
        e[1].getFullYear() +
        "/" +
        (e[1].getMonth() * 1 + 1) +
        "/" +
        e[1].getDate();
    },

    // 点击一级分类改变
    changeFirst() {
      // 根据一级分类的id请求二级分类
      // 二级分类的值先清空
      this.user.second_cateid = "";
      this.getsecondelist();
    },
  //  点击二级列表
  getGoods(){
    this.goodslist="",
    this.getgoods();
  },
    // 获取商品
    async getgoods(){
       let res = await reqgoodslist({
        fid: this.user.first_cateid,
        sid: this.user.second_cateid,
       });
       if(res.data.code==200){
         console.log(res.data.list);
         this.goodslist=res.data.list?res.data.list:[]
       }
    },

    // 计算二级列表
    async getsecondelist() {
      //await 等待成功后执行的结果 就是 reqcatelist({pid:this.user.first_cateid}).then(res=>{

      // console.log(res); }) 及成功状态下后执行的结果
      //根据一级列表id请求二级列表对应参数
      let res = await reqcatelist({ pid: this.user.first_cateid });
      if (res.data.code === 200) {
        // 二级列表请求数据赋值
        this.secondeCatelist = res.data.list ? res.data.list : [];
      }
    },
    // 规格发生改变计算规格属性
 
    // 清空user
    empty() {
      this.user = {
       title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 2,
      };
      // 二级分类列表
      (this.secondeCatelist = []), (this.attrsList = []);
      //  规格属性列表
    },
    // 检查数据
    checkedData() {
      return new Promise((resolve, reject) => {
        // 验证数据是否满足条件
        if (this.user.first_cateid === "") {
          errorAlert("一级分类不能为空");
          return;
        }
        if (this.user.second_cateid === "") {
          errorAlert("二级分类不能为空");
          return;
        }

        resolve();
      });
    },

    // 添加数据
    add() {
      this.checkedData().then(async () => {
     
        let res = await reqsecksadd(this.user);
        if (res.data.code === 200) {
          successAlert(res.data.msg);
          // 清空·user
          console.log(res);
          this.empty();
          this.info.isshow = false;
          刷新列表;
          // this.reqList();
        }
      });
    },

    getone(id) {
      reqseckinfo({ id: id }).then((res) => {
        console.log(res.data.code === 200);
        if (res.data.code == 200) {
          this.user = res.data.list;
          this.value1=[
            new Date(this.user.begintime),
            new Date(this.user.endtime)
          ]
          // 补个ID
          this.user.id = id;
          // 根据一级分类id获取二级分类列表
          this.getsecondelist();
          
        }
      });
    },
    // 修改
    updata() {
      // 实现深拷贝
      
      this.checkedData().then(async () => {
        let data = { ...this.user };
        // // 用json.parse(json.stringify())方法深拷贝会使对象丢失
        // data.specsattr = JSON.stringify(this.user.specsattr);
    
       
        reqseckedit(data).then((res) => {
          if (res.data.code == 200) {
            successAlert(res.data.msg);

            this.empty();
            this.info.isshow = false;
            // 刷新列表
            this.reqList();
          }
        });
      });
    },
    // 弹框消失
    closed() {
    
      if (!this.info.isadd) {
        this.empty();
      }
    },
    // 弹框出现
    opened() {
      //  控制子组件uploadimg中input出现
    },
  },
  mounted() {
    this.reqCateList();
    //  请求规格列表
  },
};
</script>
<style lang="less" scoped>
@import "../../../less/index.less";
.line {
  display: flex;
  .el-input {
    flex: 1;
  }
}
</style>